package com.css.devops.rest;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.css.devops.domain.DevopsComputer;
import com.css.devops.domain.DevopsPlugin;
import com.css.devops.service.DevopsApi;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Operate Basic Jenkins API Interface  
 */
@Api(tags = "Devops：操作Devops的基础API，如启动服务、启动等")
@RestController
@RequestMapping("${devops-context}/api")
public class DevopsApiResource {

	@Resource(name = "${devops.service.api}")
	private DevopsApi devopsApi;
	
	
	@ApiOperation(value = "获取Devops主机信息")
	@GetMapping("getComputerInfo")
	public ResponseEntity<List<DevopsComputer>> getComputerInfo() {
		List<DevopsComputer> devopsComputers = null;
		try {
			devopsComputers = devopsApi.getComputerInfo();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(devopsComputers);
		}
		return ResponseEntity.ok(devopsComputers);
	}
	
	@ApiOperation(value = "重启Devops服务")
	@GetMapping
	public ResponseEntity<String> restart() {
		try {
			devopsApi.restart();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("重启Devops服务失败!");
		}
		return ResponseEntity.ok("重启Devops服务成功!");
	}
	
	@ApiOperation(value = "安全重启Devops服务")
	@GetMapping("safeRestart")
	public ResponseEntity<String> safeRestart() {
		try {
			devopsApi.safeRestart();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("安全重启Devops服务失败!");
		}
		return ResponseEntity.ok("安全重启Devops服务成功!");
	}
	
	@ApiOperation(value = "安全停止Devops服务")
	@GetMapping("safeExit")
	public ResponseEntity<String> safeExit() {
		try {
			devopsApi.safeExit();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("安全停止Devops服务失败!");
		}
		return ResponseEntity.ok("安全停止Devops服务成功!");
	}
	
	@ApiOperation(value = "断开连接Devops服务")
	@GetMapping("close")
	public ResponseEntity<String> close() {
		try {
			devopsApi.close();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("断开连接Devops服务失败!");
		}
		return ResponseEntity.ok("断开连接Devops服务成功!");
	}
	
	@ApiOperation(value = "Devops是否运行")
	@GetMapping("isRunning")
	public ResponseEntity<Boolean> isRunning() {
		boolean isRunning = false;
		try {
			isRunning = devopsApi.isRunning();
		}catch (Exception e) {
			return ResponseEntity.ok(isRunning);
		}
		return ResponseEntity.ok(isRunning);
	}
	
	@ApiOperation(value = "Devops插件信息")
	@GetMapping("pluginInfo")
	public ResponseEntity<List<DevopsPlugin>> pluginInfo() {
		List<DevopsPlugin> plugins = null;
		try {
			plugins = devopsApi.getPluginInfo();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(plugins);
		}
		return ResponseEntity.ok(plugins);
	}
}
